package de.urw.core;

import java.util.ArrayList;
import java.util.Iterator;

import android.util.FloatMath;
import android.util.Log;

public class URWwall {

	public URWwall(){}
	
	
	public ArrayList<int[]> createWalls(ArrayList<URWnode> shopsold, ArrayList<URWedge> edges){
		
		ArrayList<URWnode> shops = shopsold;
		
		ArrayList<int[]> walls = new ArrayList<int[]>();
		
		float length1 = 10000;
		float actualLength = 0;
		int index;
		
		
		Iterator<URWnode> sIter = shops.iterator();
		while(sIter.hasNext()) {
			URWnode e = sIter.next();
			
			int[] wall = new int[]
			{
				e.x, e.y, 0, 0
			};
			
			//wall[0]=e.x;
			//wall[1]=e.y;
			
			sIter.remove();
			
				for(URWnode f : shops) {
					actualLength = linelength(wall[0], wall[1], f.x, f.y);
					
					Log.d("intersect", "" + intersection(wall[0], wall[1], f.x, f.y, edges));
					if ((wall[0]!=f.x || wall[1]!=f.x) && actualLength < length1 && !intersection(wall[0], wall[1], f.x, f.y, edges)) {
					//if (actualLength < length1) {
						length1 = actualLength;
						wall[2]= f.x;
						wall[3]= f.y;
					}
					
				}
			//Log.d("lego", "" + wall[0] + ", " + wall[1] + ", " + wall[2] + ", " + wall[3] + ", \n");
				
			if (wall[3] != 0 && wall[2] != 0){
				Log.d("lego", "" + wall[0] + ", " + wall[1] + ", " + wall[2] + ", " + wall[3] + ", \n");
				Log.d("line", "" + actualLength);
				walls.add(wall);
			}
			length1 = 10000;
		}
		
		for(int[] e : walls) {
			//world.addPolyline(createWall(e[0], e[1], e[2], e[3]));
			Log.d("walls","" + e[0] + ", " + e[1] + ", " + e[2] + ", " + e[3]);
		}
		
		return walls;
	}
	
	
	public boolean intersection(int x1, int y1, int x2, int y2, ArrayList<URWedge> edges) {
			for (URWedge k : edges) {
				if (k.initialized) {
					if (linesIntersect(x1, y1, x2, y2, k.source.x, k.source.y, k.sink.x, k.sink.y)) {
						return true;
					}
				}
			}
		return false;
	}
	
	
	/*public ArrayList<int[]> createWalls(ArrayList<URWnode> shopsold, ArrayList<URWedge> edges){
		ArrayList<URWnode> shops = new ArrayList<URWnode>();
		for(URWnode e : shopsold) {
			shops.add(e);
		}
		
		ArrayList<int[]> walls = new ArrayList<int[]>();
		int[] nodeOne = {0,0};
		int[] shortest1 = {0,0};
		int[] shortest2 = {0,0};
		int[] shortest3 = {0,0};
		float length1 = 10000;
		float length2 = 10000;
		float length3 = 10000;
		float actualLength;
		int[] wall = {0,0,0,0};
		
		for(URWnode e : shops) {
			nodeOne[0]=e.x;
			nodeOne[1]=e.y;
			shops.remove(shops.indexOf(e));
			
				for(URWnode f : shops) {
					actualLength = linelength(nodeOne[0], nodeOne[1], f.x, f.y);
					if (actualLength<length3 && actualLength>=length2) {
						length3 = actualLength;
						shortest3[0]=f.x;
						shortest3[1]=f.y;
					}
					if (actualLength<length2 && actualLength>=length1) {
						length3 = length2;
						shortest3[0]=shortest2[0];
						shortest3[1]=shortest2[1];
						length2 = actualLength;
						shortest2[0]=f.x;
						shortest2[1]=f.y;
					}
					if (actualLength<length2 && actualLength>=length1) {
						length3 = length2;
						shortest3[0]=shortest2[0];
						shortest3[1]=shortest2[1];
						length2 = length1;
						shortest2[0]=shortest1[0];
						shortest2[1]=shortest1[1];
						length1 = actualLength;
						shortest1[0]=f.x;
						shortest1[1]=f.y;
					}
				}
			wall[0] = nodeOne[0];
			wall[1] = nodeOne[1];
			wall[2] = shortest1[0];
			wall[3] = shortest1[1];
			if (!(wall[3] == 0 && wall[2] == 0)){
				walls.add(wall);
			}
			wall[2] = shortest2[0];
			wall[3] = shortest2[1];
			if (!(wall[3] == 0 && wall[2] == 0)){
				walls.add(wall);
			}
			wall[2] = shortest3[0];
			wall[3] = shortest3[1];
			if (!(wall[3] == 0 && wall[2] == 0)){
				walls.add(wall);
			}
			shortest3[0]=0;
			shortest3[1]=0;
			shortest2[0]=0;
			shortest2[1]=0;
			shortest1[0]=0;
			shortest1[1]=0;
		}
		
		for (int[] a : walls) {
			for (URWedge e : edges) {
				if (linesIntersect(a[0], a[1], a[2], a[3], e.source.x, e.source.y, e.sink.x, e.sink.y)) {
					walls.remove(walls.indexOf(a));
				}
			}
		}
		return walls;
	}*/
	
	
	//From Java2D API
	public static int relativeCCW(double x1, double y1,
            double x2, double y2,
            double px, double py) {
		x2 -= x1;
		y2 -= y1;
		px -= x1;
		py -= y1;
		double ccw = px * y2 - py * x2;
		if (ccw == 0.0) {
		// The point is colinear, classify based on which side of
		// the segment the point falls on.  We can calculate a
		// relative value using the projection of px,py onto the
		// segment - a negative value indicates the point projects
		// outside of the segment in the direction of the particular
		// endpoint used as the origin for the projection.
		ccw = px * x2 + py * y2;
		if (ccw > 0.0) {
		// Reverse the projection to be relative to the original x2,y2
		// x2 and y2 are simply negated.
		// px and py need to have (x2 - x1) or (y2 - y1) subtracted
		//    from them (based on the original values)
		// Since we really want to get a positive answer when the
		//    point is "beyond (x2,y2)", then we want to calculate
		//    the inverse anyway - thus we leave x2 & y2 negated.
		px -= x2;
		py -= y2;
		ccw = px * x2 + py * y2;
		if (ccw < 0.0) {
		ccw = 0.0;
		}
		}
		}
		return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0);
	}
	
	//From Java2D API
	public static boolean linesIntersect(double x1, double y1,
            double x2, double y2,
            double x3, double y3,
            double x4, double y4) {
		return ((relativeCCW(x1, y1, x2, y2, x3, y3) *
		relativeCCW(x1, y1, x2, y2, x4, y4) <= 0)
		&& (relativeCCW(x3, y3, x4, y4, x1, y1) *
		relativeCCW(x3, y3, x4, y4, x2, y2) <= 0));
	}
	
	public static float linelength(int x1, int y1, int x2, int y2) {
		return (float) Math.sqrt(Math.pow((x2-x1), 2) + Math.pow((y2-y1), 2));
	}
	
}
